using System;
using System.Collections.Generic;
using System.Text;

namespace SteeringBehaviors
{
    class Asteroid : MovingObject
    {
        #region PRIVATE MEMBERS

        static Random rand = new Random();
        float rotationSpeed;
        float maxSpeed;
        float armor;
        uint cargo;

        #endregion

        #region PROPERTIES

        /// <summary>
        /// The speed (in radians per second) that this asteroid will rotate; negative is clockwise
        /// </summary>
        public float RotationSpeed
        {
            get { return rotationSpeed; }
            set { rotationSpeed = value; }
        }

        /// <summary>
        /// The maximum pixels that this asteroid will travel per second
        /// </summary>
        public float MaxSpeed
        {
            get { return maxSpeed; }
            set { maxSpeed = value; }
        }

        /// <summary>
        /// The amount of damage that this asteroid must recieve before breaking up into smaller asteroids; negative values denote invincibilty
        /// </summary>
        public float Armor
        {
            get { return armor; }
            set { armor = value; }
        }

        /// <summary>
        /// The amount of minerals that this asteroid will provide
        /// </summary>
        public uint Cargo
        {
            get { return cargo; }
            set { cargo = value; }
        }

        #endregion

        #region CONSTRUCTORS

        /// <summary>
        /// Default constructor
        /// </summary>
        public Asteroid()
        {
            MaxSpeed = 10f;
            Position = new Microsoft.DirectX.Vector3(Convert.ToSingle(rand.NextDouble()) * Rendering.WorldSize.X,
                Convert.ToSingle(rand.NextDouble()) * Rendering.WorldSize.Y, 0);
            Microsoft.DirectX.Vector3 velocity = new Microsoft.DirectX.Vector3(Convert.ToSingle(rand.NextDouble() -.5),
                Convert.ToSingle(rand.NextDouble() - .5), 0);
            velocity.Normalize();
            velocity.Scale(MaxSpeed);
            Velocity = velocity;
            RotationSpeed = Convert.ToSingle(rand.NextDouble() * Math.PI - Math.PI/2);
            Armor = 100f;
            Cargo = 0;
            Image = "../Resources/images/asteroids/VLAsteroid.png";
            ImageDimensions = new System.Drawing.Size(128, 128);
            Dimensions = new System.Drawing.Size(128, 128);
        }

        #endregion

        #region INTERFACE METHODS

        /// <summary>
        /// Updates the position and rotation of this asteroid
        /// </summary>
        /// <param name="_delta">the amount of time passed since last Update call (in seconds)</param>
        public override void Update(float _delta)
        {
            float heading = Heading;
            base.Update(_delta);
            heading += RotationSpeed * _delta;
            Heading = heading;
        }

        /// <summary>
        /// Ensures that this asteroid will "wrap-around" when leaving the provided bounding area
        /// </summary>
        /// <param name="bounds">the provided bounding area</param>
        public virtual void BoundsCheck(Microsoft.DirectX.Vector3 bounds)
        {
            Microsoft.DirectX.Vector3 position = new Microsoft.DirectX.Vector3();
            position = Position;
            if (position.X > bounds.X + Dimensions.Width)
                position.X = -Dimensions.Width;
            if (position.X < -Dimensions.Width)
                position.X = bounds.X + Dimensions.Width;
            if (position.Y > bounds.Y + Dimensions.Height)
                position.Y = -Dimensions.Height;
            if (position.Y < -Dimensions.Height)
                position.Y = bounds.Y + Dimensions.Height;
            Position = position;
        }

        #endregion
    }
}
